/**
 * 
 */
package info.niwota.start;

import info.niwota.start.contact.Contact;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.text.TextUtils;

/**
 * @author qiangli
 * 
 */
public class Preferences {

	private static final String APP_LIST_ = "settings.app.list.";

	private static final String TAB = "\t";
	
	private static final String APP_RUNNING_STATUS = "settings.app.running.status";
	
	private static final String REDIAL_MAX = "settings.redial.max";
	
	private static final String POWERUP_LAST_APP = "powerup.last.app";

	public static String getApps(Context ctx, String key) {
		final String defvalue = null;
		try {
			final SharedPreferences pref = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			return pref.getString(APP_LIST_+key, defvalue);
		} catch (Exception e) {
		}
		return defvalue;
	}

	public static void putApps(Context context, String key, String apps) {
		try {
			final SharedPreferences pref = PreferenceManager
					.getDefaultSharedPreferences(context);
			pref.edit().putString(APP_LIST_+key, apps).commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static boolean getRunningStatus(Context ctx) {
		final boolean defvalue = false;
		try {
			final SharedPreferences pref = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			return pref.getBoolean(APP_RUNNING_STATUS, defvalue);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return defvalue;
	}
	
	public static void updateRunningStatus(Context ctx, boolean b) {
		try {
			final SharedPreferences p = PreferenceManager
			.getDefaultSharedPreferences(ctx);
			p.edit().putBoolean(APP_RUNNING_STATUS, b).commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @param ctx
	 * @return true if at least one power-up application has been selected
	 */
	public static boolean enablePowerFeature(Context ctx) {
		final boolean defvalue = false;
		try {
			final SharedPreferences pref = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			return !TextUtils.isEmpty(pref.getString(APP_LIST_+"powerup", null));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return defvalue;
	}


	public static final String CONTACT_NAME_ = "contact.name.";
	public static final String CONTACT_NOTE_ = "contact.note.";
	public static final String CONTACT_PHONE_ = "contact.phone.";

	public static Contact getContact(Context ctx, String key) throws Exception {
		final SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(ctx);
		Contact o = new Contact();
		o.name = p.getString(CONTACT_NAME_+key, null);
		o.note = p.getString(CONTACT_NOTE_+key, null);
		o.phone = p.getString(CONTACT_PHONE_+key, null);
		return o;
	}
	
	public static void putContact(Context ctx, String key, Contact o) throws Exception {
		final SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(ctx);

		Editor e = p.edit();
		e.putString(CONTACT_NAME_+key, o.name);
		e.putString(CONTACT_NOTE_+key, o.note);
		e.putString(CONTACT_PHONE_+key, o.phone);
		e.commit();
	}
	
	public static int getRedialMax(Context context) {
		int defval = 0;
		try {
			final SharedPreferences pref = PreferenceManager
			.getDefaultSharedPreferences(context);
			String s = pref.getString(REDIAL_MAX, null);
			if (s == null) {
				return defval;
			}
			return Integer.parseInt(s);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return defval;
	}

	public static Intent getLastApp(Context ctx) {
		try {
			final SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(ctx);
			
			String s = p.getString(POWERUP_LAST_APP, null);
			if (TextUtils.isEmpty(s)) {
				return null;
			}
			
			String[] sa = s.split(TAB);
			Intent i = new Intent(Intent.ACTION_MAIN);
			i.setClassName(sa[0], sa[1]);
			
			return i;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void putLastApp(Context ctx, Intent i) {
		String s = null;
		if (i != null) {
			ComponentName cn = i.getComponent(); 
			String pkg = cn.getPackageName();
			String clz = cn.getClassName();
			s = pkg+TAB+clz;
		}
		
		final SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(ctx);
		Editor e = p.edit();
		e.putString(POWERUP_LAST_APP, s);
		e.commit();
	}
}
